/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.storage;

import edu.mobbuzz.bean.Login;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;

public class LoginRecordStore {
    private final int MAX_LOGIN = 1;
    private RecordStore rsLogin;
    public Login[] loginArr;
    private int nbLogin;

    private int tempInt;
    private byte []data;

    ByteArrayOutputStream bout;
    DataOutputStream dout;

    public LoginRecordStore(){
        openRecStore();
    }
    public boolean openRecStore () {
        try {
            rsLogin = RecordStore.openRecordStore("LOGIN", true);
            loginArr = new Login[MAX_LOGIN];
            setNbLogin(rsLogin.getNumRecords());
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return true;
    }

    public boolean readRSLogin() {
        try {
            RecordEnumeration reLogin = rsLogin.enumerateRecords(null,null,false);

            reLogin.reset();
            tempInt = 0;
            while (reLogin.hasNextElement()) {
                loginArr[tempInt] = new Login();
                readLogin(reLogin.nextRecordId(), loginArr[tempInt++]);
            }
            reLogin.destroy();
        } catch (RecordStoreException e) {
                e.printStackTrace();
                return false;
        }
        return true;
    }

    public boolean readLogin( int id, Login login ){
        data = new byte[110];
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);

        if(rsLogin!= null ){
            try {
                rsLogin.getRecord( id, data, 0 );
                login.setRecId(id);
                login.setUsername(din.readUTF());
                login.setPassword(din.readUTF());
                login.setResources(din.readUTF());
                din.reset();
            } catch( Exception e ) {
                return false;
            }
        }
        return true;

    }

    public void addLogin(Login login){
        if (getNbLogin() < MAX_LOGIN) {
            try {
                bout = new ByteArrayOutputStream();
                dout = new DataOutputStream(bout);
                dout.writeUTF(login.getUsername());
                dout.writeUTF(login.getPassword());
                dout.writeUTF(login.getResources());
                dout.flush();
                data = bout.toByteArray();
                bout.reset();
                loginArr[nbLogin] = new Login();
                loginArr[getNbLogin()].setRecId(rsLogin.addRecord(data, 0, data.length ));
                loginArr[getNbLogin()].setUsername(login.getUsername());
                loginArr[getNbLogin()].setPassword(login.getPassword());
                loginArr[getNbLogin()].setResources(login.getResources());
                nbLogin++;
            } catch( Exception e ) {
                if (e instanceof RecordStoreFullException){

                }
                e.printStackTrace ();
            }
        } else {

        }
    }
    
    public int getNbLogin() {
        return nbLogin;
    }

    public void setNbLogin(int nbLogin) {
        this.nbLogin = nbLogin;
    }

    

    
}
